package bd403.utils;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import bd403.Backend.Drink;
import bd403.namespace.R;

/* Class that manages a list of drinks
 * Main author: Joe
 */

public class DrinkAdapter extends ArrayAdapter<Drink>{
	private final Activity context;
	private ArrayList<Drink> items;

	public DrinkAdapter(Activity context, int textViewResourceId, ArrayList<Drink> items) {
		super(context, R.layout.drinkitem, items);
		this.context = context;
		this.items = items;
	}
	
	/* Sets the view for a drink.
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			v = inflater.inflate(R.layout.drinkitem, null);
		}
		Drink currentDrink = items.get(position);
		if (currentDrink != null) {
			TextView drinkTitle = (TextView) v.findViewById(R.id.drinktitle);
			TextView drinkRating = (TextView) v.findViewById(R.id.drinkrating);
			ImageView iv = (ImageView) v.findViewById(R.id.icon);
			if (drinkTitle != null) {
				drinkTitle.setText(currentDrink.getName());
			}
			if (drinkRating != null) {
				int total = currentDrink.getNumRatings();
		        String ratings = context.getString(R.string.drinklabel_ratings);
		        if (total == 1) {
		        	ratings = context.getString(R.string.drinklabel_rating);
		        }
				DecimalFormat f = new DecimalFormat(context.getString(R.string.numberformat_rating));
				drinkRating.setText(context.getString(R.string.drinklabel_avgrating) + f.format(currentDrink.getRating()) +
						context.getString(R.string.drinkreview_separator) + total + ratings);
			}
			if(iv != null) {
				switch(currentDrink.getType()) {
				case 0 : 
					iv.setImageResource(R.drawable.beer_icon);
					break;
				case 1 :
					iv.setImageResource(R.drawable.wine);
					break;
				case 2 :
					iv.setImageResource(R.drawable.cocktail);
					break;
				default :
					iv.setImageResource(R.drawable.default_icon); //Change this later
				}
			}

		}
		return v;
	}
}